package com.cy.pj.common.web;

import com.cy.pj.common.exception.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**Spring MVC中的拦截器*/
public class TimeAccessInterceptor implements HandlerInterceptor {
    /**此方法在我们的后端controller方法执行之前执行*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("=preHandle==");
        LocalTime time=LocalTime.now();//LocalTime为JDK8中的API
        int hour=time.getHour();
        System.out.println("hour="+hour);
        if(hour<6||hour>23){
            throw new ServiceException("请在指定时间访问");
        }
        return true;//true标识放行，false表示请求到此结束
    }
}
